import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, ValidatorFn, ValidationErrors } from '@angular/forms';
import { MemberService } from '../member.server';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { variable } from '@angular/compiler/src/output/output_ast';

@Component({
  templateUrl: './member-center-withdrawal.component.html',
  styleUrls: ['./member-center-withdrawal.component.scss']
})
export class MemberCenterWithdrawalComponent implements OnInit {
  private validateForm: FormGroup;
  private bankForm: FormGroup;
  private _coin: any = this.storage.get('coin');
  private _coinData: any ;
  private _pageData: any = this.storage.get('coin');
  private _successPageData: any ;
  private _page: any = {
    pageSize:3
  };
  private isCollapse:boolean = true;
  private _selCard:any = true ;
  private _isShowSuccess:boolean = false;
  
  ngOnInit() {
    this.loadList();
  }
  // '获取币提现(余额,费率等)'
  
  async loadList() {
    let teg = await this.memberServer.getAJX('获取币提现(余额,费率等)',Object.assign(this._coin, this._page)).catch(err => console.log(err));
    this.memberServer.getApiSuccess(Object.assign(this._coin, this._page),false) ? this.successListOk(teg.data, '获取币提现(余额,费率等)') : '';
  };
  async getCash(toData, url) {
    let teg = await this.memberServer.getAJX(url ,toData);
    this.memberServer.getApiSuccess(teg,false) ? this.successListOk(teg.data, url) : '';
  };
  async addBank(data) {
    let teg = await this.memberServer.getAJX('创建银行',data);
    this.memberServer.getApiSuccess(teg,{bool:true,msg:'创建银行成功！'}) ? this.successListOk(teg.data,'创建银行'):'';
  };
  
  private _amount:number;
  public successListOk(data,url):void{
    console.log('创建银行')
    switch (url) {
      case '获取币提现(余额,费率等)':
        this._pageData = data;
        this._coinData = data.coin;
        this._amount = this._coinData.userWithdrawalCashMinAmount;
        this.validateForm = this.fb.group({
          fundPwd: [ null, [ Validators.required ,  Validators.pattern('^[0-9]{6}$') ] ],
          amount: [ this._amount , [ Validators.required ] ],
        });
        break;
      case '个人提现请求':
        this.getCash({withdrawalId: data.id},'个人提现详情页');
        break;
      case '个人提现详情页':
        this._isShowSuccess = true;
        this._successPageData = data;
        break;
      case '个人提现详情页':
        this._isShowSuccess = true;
        this._successPageData = data;
        break;
      case '创建银行':
          this.handleCancel();
          this.loadList();
        break;
    };
  };
  public toggleCollapse(): void {
    this.isCollapse = !this.isCollapse;
    this.isCollapse?this._page.pageSize=3 : this._page.pageSize=20;
    this.loadList();
  };
  public getAll(e):void{
    this._amount = this._pageData.availableBalance;
    if (this._amount * this._coinData.userWithdrawalCashFeePercent <  this._coinData.userWithdrawalCashFeeMin) {
      this._amount -= 0.02;
    } else {
      // tslint:disable-next-line:max-line-length
      this._amount = Number(Number(this._amount / (1 + this._coinData.userWithdrawalCashFeePercent)).toFixed(this._coinData.userWithdrawalCashAllowDecimal));
    }
  };
  public handleOk(todata):void{
    todata.bankId = this._selCard.id;
    this.getCash(todata, '个人提现请求');
  };
  public onAddBank(todata):void{
    todata.isDefault = todata.isDefault ? todata.isDefault = 'Y' : todata.isDefault = 'N' ;
    this.addBank(todata);
  };
  public selBankCard(e):void{
    switch (e) {
      case 'newCard':
      this.isVisible =true;
        break;
      default:
        if(e.isDefault == 'Y'){
          this._selCard = e;
        };
        break;
    };
  };

  isVisible = false;
  handleCancel(): void {
    this.isVisible = false;
  };

  private balanceValidators: ValidatorFn = (control: FormGroup): ValidationErrors | null => {
    const amount = control.get('amount');
    return amount > this._pageData.availableBalance ? { 'balanceError': true } : { 'balanceError': false };
  };



  constructor(
    private memberServer: MemberService,
    private fb: FormBuilder,
    private storage: StorageService
  ) { 
    this.validateForm = this.fb.group({
      fundPwd: [ null, [ Validators.required ,  Validators.pattern('^[0-9]{6}$') ] ],
      amount: [ null, [ Validators.required ] ],
    });
    this.bankForm = this.fb.group({
      bankAccountName: [ null, [ Validators.required ] ],
      bankName   : [ null, [ Validators.required ] ],
      bankSubBranch: [ null, [ ] ],
      bankAccountNum : [ null, [ Validators.required ] ],
      isDefault : [ null, [] ]
    });
  }
}
